package com.edie.customview.data.net.api;

import android.support.annotation.NonNull;

import com.edie.customview.BuildConfig;
import com.edie.customview.util.MD5;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;

/**
 * edie create on 2018/7/16
 */
public class CommonParamsInterceptor implements Interceptor {


    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {

        Request request = chain.request();

        RequestBody requestBody = request.body();
//        if (!bodyEncoded(request.headers())) {
        Map<String, String> maps = new HashMap<>();

        Buffer buffer = new Buffer();
        requestBody.writeTo(buffer);
        String s = buffer.readUtf8();
        String[] split = s.split("&");
        for (String strings : split) {
            String[] split1 = strings.split("=");
            int length = split1.length;
            String s1 = "";
            String s2 = "";
            if (length >= 1) {
                s1 = split1[0];
            }
            if (length >= 2) {
                s2 = split1[1];
            }
            maps.put(s1, s2);
        }
        maps.put("device", "android");
        maps.put("deviceid", "abcde123f");
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        maps.put("time", time);
        maps.put("version", BuildConfig.VERSION_NAME);

        List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>(maps.entrySet());
        Collections.sort(list, (o1, o2) -> o1.getKey().compareTo(o2.getKey()));
//        Collections.sort(list,new Comparator<Map.Entry<String,String>>() {
//            //升序排序
//            public int compare(Entry<String, String> o1,
//                               Entry<String, String> o2) {
//                return o1.getValue().compareTo(o2.getValue());
//            }
//        });

        Collection<String> values = maps.values();
        String hash = getHash(values);
//        }

        Request build = request.newBuilder()
                .addHeader("device", "android")
                .addHeader("deviceid", "abcde123f")
                .addHeader("time", time)
                .addHeader("version", BuildConfig.VERSION_NAME)
                .addHeader("hash", hash)
                .build();
        Response response = chain.proceed(build);
        return response;
    }

    public String getHash(Collection<String> values) {
        List<String> list = new ArrayList<>(values);
        String securityCode = "8cUoeQ31Q3C+5DQEDkYD55RA++GP0EwJgE95gaxbZlo=";
        StringBuilder sb = new StringBuilder();
        for (String s : list) {
            sb.append(s);
        }
        sb.append(securityCode);
        return MD5.getStringMD5(sb.toString());
    }


    private boolean bodyEncoded(Headers headers) {
        String contentEncoding = headers.get("Content-Encoding");
        return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity");
    }
}
